#include "timer.h"
#include "buzzer.h"
#include "led.h"

extern uint8_t LED_flag;
void Timer3_Init(uint16_t times)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = times-1;
	TIM_TimeBaseInitStruct.TIM_Prescaler = 48000-1;
	
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPriority = 3;
	NVIC_Init(&NVIC_InitStruct);
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
	TIM_ClearFlag(TIM3,TIM_FLAG_Update);
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM3,ENABLE);
}

void TIM3_IRQHandler(void)
{
	if(TIM_GetFlagStatus(TIM3,TIM_FLAG_Update) == SET)
	{
		BUZZER_OFF
		TIM_Cmd(TIM3,DISABLE);
		TIM_ClearFlag(TIM3,TIM_FLAG_Update);
	}
}

void Timer14_Init()
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = 10000;
	TIM_TimeBaseInitStruct.TIM_Prescaler = 48000-1;
	
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM14_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPriority = 3;
	NVIC_Init(&NVIC_InitStruct);
	TIM_TimeBaseInit(TIM14,&TIM_TimeBaseInitStruct);
	TIM_ClearFlag(TIM14,TIM_FLAG_Update);
	TIM_ITConfig(TIM14,TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM14,ENABLE);
}
extern uint8_t key_len;
extern int key_flag;
void TIM14_IRQHandler(void)
{
	if(TIM_GetFlagStatus(TIM14,TIM_FLAG_Update) == SET)
	{
		LED_flag = 1;
		key_len = 0;
		key_flag = 0;
		SI12TCH_LED_OFF
		TIM_Cmd(TIM14,DISABLE);
		TIM_ClearFlag(TIM14,TIM_FLAG_Update);
	}
}
